<?php
  // no direct access
  defined( '_JEXEC' ) or die( 'Restricted access' );
  jimport('joomla.application.component.controller');
  include("base.php");
  
  class DivisionController extends BaseController
  {
    var $_name = 'division';
    
    function add(){
      
      $document =& JFactory::getDocument();
      $document->addScript(JURI::root().'media/system/js/jquery.simple-color.js');
      $view = & $this->getView($this->_name, $document->getType());
      
      $view->setModel($this->getModel());
      $view->setModel($this->getModel('conference','conferencemodel'));
      parent::add();
    }
    
    function edit(){
      
      $document =& JFactory::getDocument();
      $document->addScript(JURI::root().'media/system/js/jquery.simple-color.js');
      $view = & $this->getView($this->_name, $document->getType());
      
      $view->setModel($this->getModel());
      $view->setModel($this->getModel('conference','conferencemodel'));
      $view->setModel($this->getModel('divisionconference','divisionconferencemodel'));
      parent::edit();
    }
    
    function save()
    {
      $data          = JRequest::get('POST');
      $data['infos'] = JRequest::getVar( 'infos', '', 'post', 'string', JREQUEST_ALLOWHTML );
      
      $model    = & $this->getModel($this->_name); 
      $division = $model->save($data);
      
      $division_conference = $this->getModel('divisionconference','divisionconferencemodel');
      $division_conference->renew_conferences($division->id, $data['conferences']);
       
      $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option').'&c='.JRequest::getVar('c'), false);
      $this->setRedirect($redirectTo,  ucfirst($this->_name) . ' Saved!');             
    }
    
    function remove()
    {
      $ids = JRequest::getVar('cid', null, 'default', 'array');
      if($ids === null)
        JError::raiseError(500, 'cid parameter missing from the request');
       
      $division_conference = & $this->getModel('divisionconference', 'divisionconferencemodel');
      foreach($ids as $id){
        $division_conferences = $division_conference->get_by_division_id($id);
        $dc_ids           = array();
        foreach($division_conferences as $dc)
          array_push($dc_ids, $dc->id);
        if(!empty($dc_ids))
          $division_conference->delete($dc_ids);
      }
      parent::remove();
    }
  }
?>